package com.lxit.video.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lxit.video.bean.VideoComment;
import com.lxit.video.bean.VideoInfo;
import com.lxit.video.bean.VideoType;
import com.lxit.video.service.VideoCommentService;
import com.lxit.video.service.VideoInfoService;
import com.lxit.video.service.VideoTypeService;
import com.lxit.video.util.PageUtil;

@RestController
public class VideoInfoController {

	@Autowired
	private VideoInfoService videoInfoService;

	@Autowired
	private VideoTypeService videoTypeService;

	@Autowired
	private VideoCommentService videoCommentService;

	/**
	 * 1：视频封面路径、视频名称、视频创建时间、视频喜欢次数、视频描述 2：获取视频分类id、分类名称
	 * 3：最热视频（3个，分类id、视频id、封面路径、视频名、创建时间、喜欢次数）
	 * 4：最新视频（3个，分类id、视频id、封面路径、视频名、创建时间、喜欢次数）
	 * 5：根据视频分类获取其分类的最热视频（3个，分类id、视频id、封面路径、视频名、创建时间、喜欢次数） 6：获取所有视频分类信息
	 * 
	 * @author JiangLing 2018年1月29日
	 * @param map
	 *            回显数据
	 * @param videoId
	 * @return 回显数据
	 */
	@ResponseBody
	@RequestMapping("/videoInfo/singleInitInfos")
	public ModelAndView singleInitInfos(Map<String, Object> map, String videoId,
			@RequestParam(value = "pageIndex", required = true, defaultValue = "1") Integer pageIndex,
			@RequestParam(value = "pageSize", required = true, defaultValue = "2") Integer pageSize) {
		// 视频分类id
		Integer voTypeId = null;
		if (null != videoId && !"".equals(videoId.trim())) {
			Integer vId = Integer.valueOf(videoId);

			VideoInfo videoInfo = videoInfoService.selectByVideoId(vId);
			if (null != videoInfo) {
				voTypeId = videoInfo.getvTypeId();

				VideoType videoType = videoTypeService.findByVoTypeId(voTypeId);

				// 最热
				List<VideoInfo> hottestVideos = videoInfoService.selectHottestVideos();
				// 最新
				List<VideoInfo> newestVideos = videoInfoService.selectNewestVideos();
				// 分类最热
				List<VideoInfo> typeHottestVideos = videoInfoService.selectTypeHottestVideos(voTypeId);
				// 所有分类
				List<VideoType> videoTypes = videoTypeService.findAll();
				// 所有评论
				PageUtil<VideoComment> pageUtil = videoCommentService.pageVideoComment(pageIndex, pageSize, vId);

				// 数据回显
				map.put("videoInfo", videoInfo);
				map.put("videoType", videoType);
				map.put("hottestVideos", hottestVideos);
				map.put("newestVideos", newestVideos);
				map.put("typeHottestVideos", typeHottestVideos);
				map.put("videoTypes", videoTypes);
				map.put("pageUtil", pageUtil);
			}
		}
		return new ModelAndView("single");
	}
}
